package com.hz.foodstalls.controller;

import java.io.IOException;
import java.util.List;

import com.hz.foodstalls.interceptor.AdminInterceptor;
import com.hz.foodstalls.model.AdminRole;
import com.hz.foodstalls.model.Resource;
import com.hz.foodstalls.model.Role;
import com.hz.foodstalls.model.RoleResource;
import com.jfinal.aop.Before;
import com.jfinal.ext.plugin.shiro.JdbcShiroPlugin;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

/**
 * RoleController, Generated by Robot on 2016-9-21 11:15:23.
 */
@Before(AdminInterceptor.class)
@ControllerBind(controllerKey = "/role", viewPath = "/WEB-INF/views/role/")
public class RoleController extends BaseController {

    private final String MODULE_NAME = "/role";

    public void index() {
        Page<Role> page = Role.dao.page(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10));
        setAttr("page", page);
        render("index.html");
    }

    public void save() {
        Role model = getModel(Role.class, "");
        boolean isSave = model.getId() == null;

        if (Role.dao.saveOrUpdate(model, isSave)) {
            JdbcShiroPlugin.reSetShiro(this);
            redirect(MODULE_NAME);
        } else
            renderError(501);
    }

    public void resourceAssignment() {
        Role role = Role.dao.findById(getParaToInt("id", 0));
        setAttr("role", role);
        render("resourceAssignment.html");

    }

    public void resourcetree() throws IOException {
        Role role = Role.dao.findById(getParaToInt("id", 0));
        if (role == null)
            role = new Role();
        List<Resource> hasresources = Resource.dao
                .find("select* from resource where id in (select resourceSet_id from role_resource where roleSet_id=+"
                        + role.getId() + ")");
        List<Resource> allresources = Resource.dao.find("select*from resource");
        StringBuilder sb = new StringBuilder("[");
        for (Resource resource : allresources) {
            if (!hasresources.contains(resource)) {
                sb.append("{\"id\":" + resource.getId() + ",\"pId\":" + resource.getPid() + ",\"name\":\""
                        + resource.getName() + "\"},");
            } else {
                sb.append("{\"id\":" + resource.getId() + ",\"pId\":" + resource.getPid() + ",\"name\":\""
                        + resource.getName() + "\",\"checked\":true,\"open\":true},");

            }
        }
        String s = sb.substring(0, sb.length() - 1) + "]";
        renderText(s);

    }

    public void edit() {
        Role role = Role.dao.findById(getParaToInt("id", 0));
        if (role == null)
            role = new Role();

        setAttr("role", role);
        render("edit.html");
    }

    public void delete() {
        String[] roleSetIds = getParaValues("ids");
        StringBuilder success = new StringBuilder();
        StringBuilder error = new StringBuilder();
        StringBuilder warn = new StringBuilder();
        List<AdminRole> adminroles = null;
        for (String roleSetId : roleSetIds) {
            adminroles = AdminRole.dao.find("select* from admin_role where roleSet_id=?", roleSetId);
            Role role = Role.dao.findById(roleSetId);
            if (adminroles == null || adminroles.isEmpty()) {
                boolean roledelete = Role.dao.deleteById(roleSetId);
                if (roledelete) {
                    List<RoleResource> roleresources = RoleResource.dao
                            .find("select * from role_resource where roleSet_id=?", roleSetId);
                    if (roleresources != null && !roleresources.isEmpty()) {
                        for (RoleResource roleResource : roleresources) {
                            Resource resource = Resource.dao.findById(roleResource.getResourcesetId());
                            if (RoleResource.dao.deleteById(roleResource.getId()))
                                success.append("角色名[" + role.getName() + "]以及其权限[" + resource.getName() + "]删除成功 ");
                            else
                                error.append("角色名[" + role.getName() + "]以及其权限[" + resource.getName() + "]删除失败   ");

                        }

                    } else {
                        success.append("角色名[" + role.getName() + "]删除成功");
                    }
                } else {
                    error.append("角色名[" + role.getName() + "]删除失败");
                }

            } else {

                error.append("角色名[" + role.getName() + "]删除失败,该角色已被绑定,请先解除绑定     ");
            }
        }
        if (error == null || error.length() == 0) {
            ajaxJsonSuccessMessage(success.toString());
        } else {
            ajaxJsonErrorMessage(error.toString() + "  " + success.toString());
        }

    }
}
